﻿var xmlhttp = false;

function SendRequestAsync(url, callback, onerror, ontimeout) {
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (xmlhttp) {
        if (callback != null)
            xmlhttp.onreadystatechange = callback;
        if (onerror != null)
            xmlhttp.onerror = onerror;
        if (ontimeout != null)
            xmlhttp.ontimeout = ontimeout;
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
    }
}

function getChart(chartType) {
    document.getElementById("divchartbyuser").innerHTML = ' <img src="img/waiting.gif" alt="Получение данных..."/>';
    var paramstring = document.location.href.split("?")[1];
    if (paramstring != undefined)
        paramstring += "&";
    else
        paramstring = '';
    paramstring += "charttype=" + chartType;

    var charturl = "chartbyuser.wcmd?" + paramstring;
    SendRequestAsync(charturl, getChartCallback, getCharton, getChartonTimeout);
}

function getChartonTimeout() {
    document.getElementById("divchartbyuser").innerHTML = "Время ожидания вышло";
}

function getCharton(e) {
    document.getElementById("divchartbyuser").innerHTML = e.target.status;
}

function getChartCallback() {
    //create XMLDOM object if xmlhttp shows "loaded"
    if (xmlhttp.readyState == 4) {
        // if "OK"
        if (xmlhttp.status == 200) {
            document.getElementById("divchartbyuser").innerHTML = xmlhttp.responseText;
        } else {
            document.getElementById("divchartbyuser").innerHTML = "Не удалось загрузить данные";
        }
    }
}

function auth() {
    var url = "auth.htm?webcommand=auth&login=" + document.getElementById("login").value +
    "&pass=" + document.getElementById("pass").value;
    SendRequestAsync(url, authCallback, null, null);
}

function authCallback() {
    document.getElementById("divauth").innerHTML = xmlhttp.responseText;
}

function logout() {
    var url = "auth.htm?webcommand=logout";
    SendRequestAsync(url, logoutCallback, null, null);
}

function logoutCallback() {
    document.getElementById("divauth").innerHTML = xmlhttp.responseText;
}

function savegeneralsettcallback() {
    document.getElementById("GeneralSettTab").innerHTML = xmlhttp.responseText;
}

function savesyslogsettcallback() {
    document.getElementById("SyslogSettTab").innerHTML = xmlhttp.responseText;
}

function savewebserversettcallback() {
    document.getElementById("WebSettTab").innerHTML = xmlhttp.responseText;
}

function saveuserscallback() {
    document.getElementById("UserSettTab").innerHTML = xmlhttp.responseText;
}